//
//  DR_SearchFileCell.swift
//  DocReaderRP
//
//  Created by  mac on 2025/11/10.
//

import UIKit
import SnapKit

struct DR_SearchFileModel {
    let fileName: String
    let date: String
    let size: String
    var isFavorite: Bool
    let fileType: String // PDF, Word, Excel, PPT
}

extension DR_SearchFileModel {
    init(from fileModel: DR_FileModel) {
        self.fileName = fileModel.fileName
        
        // 格式化日期
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        self.date = dateFormatter.string(from: fileModel.createDate)
        
        self.size = fileModel.fileSizeString
        self.isFavorite = DR_FileManager.shared.isFavorite(fileModel)
        self.fileType = fileModel.fileType.rawValue
    }
}

class DR_SearchFileCell: UITableViewCell {
    
    static let identifier = "DR_SearchFileCell"
    
    var onFavoriteTapped: (() -> Void)?
    var onMoreTapped: (() -> Void)?
    
    private let fileIconView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "folder_icon")
        imageView.contentMode = .scaleAspectFit
        imageView.layer.cornerRadius = 8
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    private let fileNameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        label.textColor = .black
        return label
    }()
    
    private let metadataLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = "#ADADAD".toColor
        return label
    }()
    
    private let favoriteButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "home_collect"), for: .normal)
        button.setImage(UIImage(named: "home_collected"), for: .selected)
        return button
    }()
    
    private let moreButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "home_more"), for: .normal)
        return button
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        setupConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(fileIconView)
        contentView.addSubview(fileNameLabel)
        contentView.addSubview(metadataLabel)
        contentView.addSubview(favoriteButton)
        contentView.addSubview(moreButton)
        
        favoriteButton.addTarget(self, action: #selector(favoriteButtonTapped), for: .touchUpInside)
        moreButton.addTarget(self, action: #selector(moreButtonTapped), for: .touchUpInside)
    }
    
    private func setupConstraints() {
        fileIconView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(60)
        }
        
        fileNameLabel.snp.makeConstraints { make in
            make.leading.equalTo(fileIconView.snp.trailing).offset(12)
            make.trailing.lessThanOrEqualTo(favoriteButton.snp.leading).offset(-12)
            make.top.equalToSuperview().offset(12)
        }
        
        metadataLabel.snp.makeConstraints { make in
            make.leading.equalTo(fileNameLabel)
            make.trailing.lessThanOrEqualTo(favoriteButton.snp.leading).offset(-12)
            make.top.equalTo(fileNameLabel.snp.bottom).offset(4)
            make.bottom.equalToSuperview().offset(-12)
        }
        
        favoriteButton.snp.makeConstraints { make in
            make.trailing.equalTo(moreButton.snp.leading).offset(-16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        moreButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
    }
    
    @objc private func favoriteButtonTapped() {
        favoriteButton.isSelected.toggle()
        onFavoriteTapped?()
    }
    
    @objc private func moreButtonTapped() {
        onMoreTapped?()
    }
    
    func configure(with model: DR_SearchFileModel) {
        fileNameLabel.text = model.fileName
        metadataLabel.text = "\(model.date) \(model.size)"
        favoriteButton.isSelected = model.isFavorite        
        fileIconView.image = UIImage(named: "folder_icon")
//        // 根据文件类型设置图标
//        switch model.fileType {
//        case "PDF":
//            fileIconView.image = UIImage(named: "home_pdf")
//        case "Word":
//            fileIconView.image = UIImage(named: "home_word")
//        case "Excel":
//            fileIconView.image = UIImage(named: "home_excel")
//        default:
//            fileIconView.image = UIImage(named: "home_lock")
//        }
    }
}

